package mini_core;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import javax.swing.plaf.synth.SynthSeparatorUI;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import com.google.gson.Gson;

public class Mymongodb {
	
	private String path = null;
	
	public Mymongodb(String path){
		this.path = path;
	}
	/**
	 * 创建数据库
	 * use()
	 * @throws Exception 
	 */
	public void use(String name) throws Exception{
		if(this.path==null){
			throw new Exception("path 不可为 null");
		}
		FileOutputStream fileOutputStream = new FileOutputStream(path);
		Document document = new Document();
		Element dataBase = new Element("database");
		Element dataName = new Element("table");
		dataName.setAttribute("name", name);
		Element indexs = new Element("table");
		indexs.setAttribute("name", "system.indexs");
		Element users = new Element("table");
		users.setAttribute("name", "system.users");
		dataBase.addContent(dataName);
		dataBase.addContent(indexs);
		dataBase.addContent(users);
		document.setRootElement(dataBase);
		
		XMLOutputter xmlOutputter = new XMLOutputter();
		xmlOutputter.output(document, fileOutputStream);
		System.out.println("创建成功");
	}
	/**
	 * 创建集合或者添加信息
	 * insert()
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public boolean insert(String name,Object o) throws JDOMException, IOException{
		boolean flag;
		Gson gson = new Gson();
		FileInputStream fileInputStream = new FileInputStream(path);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document build = saxBuilder.build(fileInputStream);
		Element rootElement = build.getRootElement();
		List<Element> children = rootElement.getChildren();
		for (Element element : children) {
			if(element.getAttributeValue("name").equals(name)){
				HashMap<String, String> hashMap = new HashMap<String, String>();
				hashMap.put("uuid", UUID.randomUUID().toString());
				String json = gson.toJson(o);
				HashMap<String,String> fromJson = gson.fromJson(json, HashMap.class);
				hashMap.putAll(fromJson);
				Element data = new Element("data");
				data.addContent(gson.toJson(hashMap));
				element.addContent(data);
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.output(build, new FileOutputStream(path));
		flag = true;
		System.out.println("insert success");
		return flag;
	}
	/**
	 * 修改信息
	 * update()
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public void update(String name,Object fo,Object o) throws JDOMException, IOException{
		ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
		Gson gson = new Gson();
		FileInputStream fileInputStream = new FileInputStream(path);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build(fileInputStream);
		Element rootElement = document.getRootElement();
		List<Element> children = rootElement.getChildren();
		for (Element element : children) {
			if(element.getAttributeValue("name").equals(name)){
				
				String json = gson.toJson(fo);
				HashMap<String,String> fromJson = gson.fromJson(json, HashMap.class);
				List<Element> children2 = element.getChildren();
				for (Element element2 : children2) {
					String text = element2.getText();
					HashMap<String,String> dataJson = gson.fromJson(text, HashMap.class);
					boolean flag = false;
					for (Entry<String, String> entry : fromJson.entrySet()) {
						if(dataJson.get(entry.getKey()).equals(entry.getValue())){
							flag = true;
							continue;
						}else{
							flag = false;
							break;
						}
					}
					
					if(flag){
						String ojson = gson.toJson(o);
						HashMap<String,String> ofromJson = gson.fromJson(ojson, HashMap.class);
						dataJson.putAll(ofromJson);	
						element2.removeContent();
						//element2.addContent(gson.toJson(dataJson));
						element2.setText(gson.toJson(dataJson));
						XMLOutputter xmlOutputter = new XMLOutputter();
						xmlOutputter.output(document, new FileOutputStream(path));
						System.out.println("修改成功");
						break;
					}
				}
				System.out.println("修改不成功");
				break;
				
			}
		}

		
	}
	/**
	 * 删除信息
	 * delete()
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public void delete(String name,Object o) throws JDOMException, IOException{
		Gson gson = new Gson();
		FileInputStream fileInputStream = new FileInputStream(path);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document build = saxBuilder.build(fileInputStream);
		Element rootElement = build.getRootElement();
		List<Element> children = rootElement.getChildren();
		String ojson = gson.toJson(o);
		HashMap<String,String> ofromJson = gson.fromJson(ojson, HashMap.class);
		for (Element element : children) {
			if(element.getAttributeValue("name").equals(name)){
				List<Element> children2 = element.getChildren();
				for (Element element2 : children2) {
					String text = element2.getText();
					//String json = gson.toJson(text);
					HashMap<String,String> fromJson = gson.fromJson(text, HashMap.class);
					boolean flag = true;
					for (Entry<String, String> entry : ofromJson.entrySet()) {
						if(!fromJson.get(entry.getKey()).equals(entry.getValue())){
							flag = false;
							break;
						}
					}
					if(flag){
						element.removeContent(element2);
						XMLOutputter xmlOutputter = new XMLOutputter();
						xmlOutputter.output(build, new FileOutputStream(path));
						System.out.println("删除成功");
						break;
					}					
				}
			}
		}
	}
	
	/**
	 * 查询全部数据
	 * findAll()
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public ArrayList<HashMap<String, String>> findAll(String name) throws JDOMException, IOException{
		Gson gson = new Gson();
		FileInputStream fileInputStream = new FileInputStream(path);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document build = saxBuilder.build(fileInputStream);
		Element rootElement = build.getRootElement();
		List<Element> children = rootElement.getChildren();
		ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String,String>>();
		for (Element element : children) {
			if(element.getAttributeValue("name").equals(name)){
				List<Element> children2 = element.getChildren();
				for (Element element2 : children2) {
					String text = element2.getText();
					HashMap fromJson = gson.fromJson(text, HashMap.class);
					arrayList.add(fromJson);
				}
				return arrayList;
			}
		}
		return arrayList;
		
	}
	
}
